package com.hxd.otc.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.flyco.dialog.widget.base.BaseDialog;
import com.hxd.otc.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lzy on 2018/6/7 0007.
 * description ${
 * 1.用于收款方式页面禁用支付方式时的对话框
 * 2.可以拓展使用于信息提示对话框
 * 3.当需要交互数据操作时，可以通过泛型传递数据
 * }
 */
public class ForbiddenBankCardDialog<T> extends BaseDialog<ForbiddenBankCardDialog<T>> {

    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_message)
    TextView mTvMessage;
    @BindView(R.id.tv_cancel)
    TextView mTvCancel;
    @BindView(R.id.tv_confirm)
    TextView mTvConfirm;

    private OnConfirmClickListener<T> mOnConfirmClickListener;
    private T mData;
    private String mTitle;
    private String mMessage;
    private String mCancleText;
    private String mConfirmText;
    private int mMessageTextGravity = Gravity.LEFT;

    public ForbiddenBankCardDialog(Context context) {
        super(context);
    }

    @Override
    public View onCreateView() {
        widthScale(0.67f);
        return View.inflate(getContext(), R.layout.dialog_forbidden_bankcard, null);
    }

    @Override
    public void onViewCreated(View view) {
        ButterKnife.bind(this, view);
    }

    @Override
    public void setUiBeforShow() {
        mTvMessage.setText(mMessage);
        mTvMessage.setGravity(mMessageTextGravity);
        if (!TextUtils.isEmpty(mTitle)) mTvTitle.setText(mTitle);
        if (!TextUtils.isEmpty(mCancleText)) mTvCancel.setText(mCancleText);
        if (!TextUtils.isEmpty(mConfirmText)) mTvConfirm.setText(mConfirmText);
    }

    @OnClick({R.id.tv_cancel, R.id.tv_confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_confirm:
                onConfirm();
                if (isConfirmDissmiss) {
                    dismiss();
                }
                break;
        }
    }

    private void onConfirm() {
        if (mOnConfirmClickListener != null) mOnConfirmClickListener.onConfirm(mData);
        dismiss();
    }

    public ForbiddenBankCardDialog<T> setOnConfirmClickListener(OnConfirmClickListener<T> onConfirmClickListener) {
        mOnConfirmClickListener = onConfirmClickListener;
        return this;
    }

    public ForbiddenBankCardDialog<T> setData(T t) {
        mData = t;
        return this;
    }

    public ForbiddenBankCardDialog<T> setTitle(String title) {
        mTitle = title;
        return this;
    }

    public ForbiddenBankCardDialog<T> setMessage(String message) {
        mMessage = message;
        return this;
    }

    public ForbiddenBankCardDialog<T> setCancleText(String cancle) {
        mCancleText = cancle;
        return this;
    }

    public ForbiddenBankCardDialog<T> setConfirmText(String confirm) {
        mConfirmText = confirm;
        return this;
    }

    public ForbiddenBankCardDialog<T> setMessageGravity(int gravity) {
        mMessageTextGravity = gravity;
        return this;
    }

    public interface OnConfirmClickListener<T> {
        void onConfirm(T data);
    }

    private boolean isConfirmDissmiss;

    public ForbiddenBankCardDialog<T> setConfirmAutoDissmiss() {
        isConfirmDissmiss = true;
        return this;
    }
}
